package domd1;

import javafx.scene.layout.Priority;

import java.util.Comparator;
import java.util.PriorityQueue;

public class Test {
    //最小k个数---小堆跟
    public int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> q=new PriorityQueue<>();

        for (int i = 0; i < arr.length; i++) {
            q.offer(arr[i]);
        }
        int[] ret=new int[k];
        for (int i = 0; i < k; i++) {
            ret[i]=q.poll();
        }
        return ret;
    }
    //最小k个数--大堆跟
    class intcmp implements Comparator<Integer> {
        @Override
        public int compare(Integer o1,Integer o2) {
            return o2.compareTo(o1);
        }
    }

    public int[] bigestK(int[] arr, int k) {
        int[] ret=new int[k];
        if(arr==null||k==0){
            return ret;
        }
        PriorityQueue<Integer> q=new PriorityQueue<>(k,new intcmp());
        for (int i = 0; i <k ; i++) {
            q.offer(arr[i]);
        }
        for (int i = k; i <arr.length ; i++) {
            int peel=q.peek();
            if(arr[i]<peel){
                q.poll();
                q.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i]=q.poll();
        }
        return ret;
    }
    public static void main(String[] args) {
        Test test=new Test();
        int[] arrays={3,6,40,321,1,6,4};
        PriorityQueue<Integer> priorityQueue =new PriorityQueue<>();
            int[] ret=test.bigestK(arrays,4);
        for (int i = 0; i <ret.length ; i++) {
            System.out.print(ret[i]+" ");
        }
    }
}
